package com.app.lxx.pregnancyberth.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

/**
 * Created by Lxx on pregnancyberth
 * 2019/6/27 16:16 星期四
 */
public class LocalBroadcastUtils {

    private Context context;

    private resultLocalBroadcast broadcast;

    public LocalBroadcastUtils(Context context) {
        this.context = context;
    }

    public interface resultLocalBroadcast {
        void resultLocalBroadcastData(Context context, Intent intent);
    }

    public void setResultLocalBroadcast(resultLocalBroadcast broadcast) {
        this.broadcast = broadcast;
    }

    public void initLocalBroadcast(String INTENT_BROADCAST) {
        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(context);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(INTENT_BROADCAST);
        BroadcastReceiver bordcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //信息处理
                broadcast.resultLocalBroadcastData(context, intent);
            }
        };
        broadcastManager.registerReceiver(bordcastReceiver, intentFilter);
    }

    public void startLocalBroadcast(String INTENT_BROADCAST) {
        LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(INTENT_BROADCAST));
    }


}
